<template>
  <el-menu
    background-color="#444c54"
    text-color="#fff"
    active-text-color="#ffd04b"
    :collapse="isCollapse"
  >
    <h3>{{ isCollapse ? "后台" : "后台管理" }}</h3>
    <el-menu-item
      v-for="item in noChildren"
      :key="item.name"
      :index="item.name"
      @click="clickMenu(item)"
    >
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu
      v-for="item in hasChildren"
      :key="item.label"
      :index="item.label"
    >
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group v-for="subItem in item.children" :key="subItem.path">
        <el-menu-item @click="clickMenu(subItem)" :index="subItem.path">{{
          subItem.label
        }}</el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-menu {
  height: 105vh;
  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400;
  }
  border-right: 0px;
}
</style>

<script>
export default {
  data() {
    return {
      menuData: [
        {
          path: "/",
          name: "home",
          label: "首页",
          icon: "s-home",
          url: "Home/Home",
        },
        {
          label: "药品明细管理",
          icon: "first-aid-kit",
          children: [
            {
              path: "/drugInfo",
              name: "drugInfo",
              label: "药品信息",
              icon: "setting",
              url: "drugInfo/drugInfo",
            },
            {
              path: "/supplierInfo",
              name: "supplierInfo",
              label: "供应商信息",
              icon: "setting",
              url: "drugInfo/drugInfo",
            },
          ],
        },
        {
          label: "药品仓库管理",
          icon: "box",
          children: [
            {
              path: "/warehouseInfo",
              name: "warehouseInfo",
              label: "仓库信息",
              icon: "setting",
              url: "warehouse/warehouseInfo",
            },
            {
              path: "/drugProblemInfo",
              name: "drugProblemInfo",
              label: "问题药品管理",
              icon: "setting",
              url: "warehouse/drugProblemInfo",
            },

            {
              path: "/warehouseRetInfo",
              name: "warehouseRetInfo",
              label: "返厂信息",
              icon: "setting",
              url: "warehouse/warehouseRetInfo",
            },
            {
              path: "/whPrchsInfo",
              name: "whPrchsInfo",
              label: "进货信息",
              icon: "setting",
              url: "warehouse/whPrchsInfo",
            },
          ],
        },
        {
          label: "销售管理",
          icon: "notebook-2",
          children: [
            {
              path: "/orderInfo",
              name: "orderInfo",
              label: "订单管理",
              icon: "setting",
              url: "order/orderInfo",
            },
            {
              path: "/userRetInfo",
              name: "userRetInfo",
              label: "用户退货管理",
              icon: "setting",
              url: "order/userRetInfo",
            },
          ],
        },
      ],
    };
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    //点击菜单
    clickMenu(item) {
      //页面的路由与跳转路由不一致时才跳转
      if (
        this.$route.path != item.path &&
        !(this.$route.path === "/home" && item.path === "/")
      ) {
        this.$router.push(item.path);
      }
      this.$store.commit("selectMenu", item);
    },
  },
  computed: {
    //没有子菜单
    noChildren() {
      return this.menuData.filter((item) => !item.children);
    },
    //有子菜单
    hasChildren() {
      return this.menuData.filter((item) => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
  },
};
</script>
